/*
 * @Descripttion: 
 * @Author: zhaozheng
 * @Version: 0.0.1
 * @Date: 2023-12-26 20:04:51
 * @LastEditors: zhaozheng
 * @LastEditTime: 2023-12-26 23:07:49
 */
// 导入 express
const express = require('express')
const schedule = require('node-schedule')
// 创建 web 服务器
const app = express()
const bodyParser = require('body-parser');
const cors = require("cors");
const exec = require('child_process').exec;

exec('start http://127.0.0.1');

app.use(cors()); //使用cors中间件

app.use(bodyParser.json()); // 在请求处理之前，先解析JSON


// 设置首页地址
app.use(express.static("./"))
app.get('/', (req, res) => {
  res.send('请求成功')
})
app.post('/save', (req, res) => {
  let date = req.body.date
  let time = req.body.time
  let fileUrl = req.body.fileUrl.replace(/\\/g, "/")
  let streamUrl = req.body.streamUrl
  let newDate = new Date(`${date} ${time}`)
  // 添加定时任务
  const job = schedule.scheduleJob(newDate, function() {
    // const {execSync} = require('child_process')
    // // execSync(`${fileUrl}`)
    // execSync(`ffmpeg -i ${fileUrl} -vcodec copy -s 1920*1080 -b:v 6144k -acodec aac -b:a 128k -f flv ${streamUrl}`,{stdio:'inherit'})
    console.log(`${date} ${time}---------${req.body.fileUrl}---------推流开始`)
    const buildExec = exec(`ffmpeg -i ${fileUrl} -vcodec copy -s 1920*1080 -b:v 6144k -acodec aac -b:a 128k -f flv ${streamUrl}`,(error, stdout, stderr) => {
      console.log(`${req.body.fileUrl}---------推流结束`)
    });
    // buildExec.stdout.on('data', function(data) {
    //   // 此处会实时打印输出
    //   console.log(data.toString()) 
    // });
    buildExec.stderr.on('data', function(data) {
      // 此处会实时打印输出
      console.log(data.toString()) 
    });
  });
  res.send({
    code: 200,
    msg: '保存成功',
    data: {
      time: `${date} ${time}`,
      fileUrl: req.body.fileUrl,
      streamUrl: streamUrl
    }
  })
})
// 3、启动 web
app.listen(80, () =>{
  console.log('express server running at http://127.0.0.1')
})




